package com.ruoyi.workbench.utils;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.workbench.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * 生成器
 *
 * @author hanjiafeng
 * @date 2021年2月26日 11点28分
 */
public class Generator {

    private static final Logger LOGGER = LoggerFactory.getLogger(Generator.class);

    public static SysDept initDept(String deptId) {
        SysDept dept = null;
        try {
            Long id = Long.valueOf(deptId);
            dept = new SysDept();
            dept.setDeptId(id);
        } catch (NumberFormatException e) {
            LOGGER.info("无单元id");
        }
        return dept;
    }

    public static WbWholeVisitLog generateWholeVisitLog(WbVisitLog wbVisitLog, HttpServletRequest httpServletRequest) {
        WbWholeUnit wholeUnit = generateWholeUnit(httpServletRequest);
        String unit7Id = httpServletRequest.getParameter("unit7Id");
        WbLeafUnit unit7 = null;
        try {
            Long id = Long.valueOf(unit7Id);
            unit7 = new WbLeafUnit();
            unit7.setUnitId(id);
        } catch (NumberFormatException e) {
            LOGGER.info("无七级单元id");
        }
        wholeUnit.setUnit7(unit7);
        WbWholeVisitLog wbWholeVisitLog = new WbWholeVisitLog();
        wbWholeVisitLog.setWholeUnit(wholeUnit);
        wbWholeVisitLog.setWbVisitLog(wbVisitLog);

        return wbWholeVisitLog;
    }

    public static WbWholeUnit generateWholeUnit(HttpServletRequest httpServletRequest) {
        String unitId3 = httpServletRequest.getParameter("unitId3");
        SysDept unit3 = initDept(unitId3);
        String unitId4 = httpServletRequest.getParameter("unitId4");
        SysDept unit4 = initDept(unitId4);
        String unitId5 = httpServletRequest.getParameter("unitId5");
        SysDept unit5 = initDept(unitId5);
        String unitId6 = httpServletRequest.getParameter("unitId6");
        SysDept unit6 = initDept(unitId6);
        WbWholeUnit wholeUnit = new WbWholeUnit();
        wholeUnit.setUnit3(unit3);
        wholeUnit.setUnit4(unit4);
        wholeUnit.setUnit5(unit5);
        wholeUnit.setUnit6(unit6);
        return wholeUnit;
    }

    /**
     * 查找str中,的数量
     *
     * @param str str
     * @return 数量
     */
    public static int getCommonCount(String str) {
        int start = str.startsWith(",") ? 1 : 0;
        int end = str.endsWith(",") ? 1 : 0;
        String[] tempArray = str.split(",");
        return tempArray.length - 1 + start + end;
    }

    public static Long parseLong(HttpServletRequest httpServletRequest, String param) {
        Long l = null;
        try {
            l = Long.valueOf(httpServletRequest.getParameter(param));
        } catch (NumberFormatException e) {
            LOGGER.info(param + "不存在");
        }
        return l;
    }

    public static Byte parseByte(HttpServletRequest httpServletRequest, String param) {
        Byte b = null;
        try {
            b = Byte.valueOf(httpServletRequest.getParameter(param));
        } catch (NumberFormatException e) {
            LOGGER.info(param + "不存在");
        }
        return b;
    }

    public static SysDept initDept(Long deptId) {
        SysDept dept = new SysDept();
        dept.setDeptId(deptId);
        return dept;
    }

    public static WbPhysicalUnit initPhysicalUnit(String idStr) {
        WbPhysicalUnit wbPhysicalUnit = null;
        try {
            Long id = Long.valueOf(idStr);
            wbPhysicalUnit = new WbPhysicalUnit();
            wbPhysicalUnit.setId(id);
        } catch (NumberFormatException e) {
            LOGGER.info("无单元id");
        }
        return wbPhysicalUnit;

    }

    public static WbWholeUnitNew generateWbWholeUnitNew(HttpServletRequest httpServletRequest) {
        String unit3Id = httpServletRequest.getParameter("unit3Id");
        SysDept unit3 = initDept(unit3Id);
        String unit4Id = httpServletRequest.getParameter("unit4Id");
        SysDept unit4 = initDept(unit4Id);
        String unit5Id = httpServletRequest.getParameter("unit5Id");
        SysDept unit5 = initDept(unit5Id);
        if (!Objects.isNull(unit5)) {
            //设置条线
            Byte type = parseByte(httpServletRequest, "type");
            unit5.setType(type);
        }
        String unit6Id = httpServletRequest.getParameter("unit6Id");
        WbPhysicalUnit unit6 = initPhysicalUnit(unit6Id);
        String unit7Id = httpServletRequest.getParameter("unit7Id");
        WbPhysicalUnit unit7 = initPhysicalUnit(unit7Id);
        String unit8Id = httpServletRequest.getParameter("unit8Id");
        WbPhysicalUnit unit8 = initPhysicalUnit(unit8Id);
        String unit9Id = httpServletRequest.getParameter("unit9Id");
        WbPhysicalUnit unit9 = initPhysicalUnit(unit9Id);
        WbWholeUnitNew wbWholeUnitNew = new WbWholeUnitNew();
        wbWholeUnitNew.setUnit3(unit3);
        wbWholeUnitNew.setUnit4(unit4);
        wbWholeUnitNew.setUnit5(unit5);
        wbWholeUnitNew.setUnit6(unit6);
        wbWholeUnitNew.setUnit7(unit7);
        wbWholeUnitNew.setUnit8(unit8);
        wbWholeUnitNew.setUnit9(unit9);
        return wbWholeUnitNew;

    }

    public static Integer parseInteger(HttpServletRequest httpServletRequest, String str) {
        Integer integer = null;
        try {
            integer = Integer.parseInt(httpServletRequest.getParameter(str));
        } catch (NumberFormatException e) {
            LOGGER.warn(str + "不存在");
        }
        return integer;
    }

    /**
     * 初始化日期
     *
     * @param httpServletRequest httpServletRequest
     * @return 日期
     */
    public static DateEntity initDateUtil(HttpServletRequest httpServletRequest) {
        Integer period = Generator.parseInteger(httpServletRequest, "period");
        period = period == -1 ? null : period;
        String startTimeStr = null;
        String endTimeStr = null;
        if (Objects.isNull(period)) {
            startTimeStr = httpServletRequest.getParameter("startTime");
            endTimeStr = httpServletRequest.getParameter("endTime");
        } else if (period == 1) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            Date yesterday = calendar.getTime();
            String timeStr = simpleDateFormat.format(yesterday);
            startTimeStr = timeStr;
            endTimeStr = timeStr;
        }
        return new DateEntity(period, startTimeStr, endTimeStr);
    }
}
